package vn.allmobile.happyvoucher.activity;

import vn.allmobile.happyvoucher.R;
import vn.allmobile.happyvoucher.loader.BaseLoader;
import vn.allmobile.happyvoucher.loader.BaseResult;
import vn.allmobile.happyvoucher.loader.HomeHotLoader;
import vn.allmobile.happyvoucher.loader.HomeSliderLoader;
 
import vn.allmobile.happyvoucher.ui.EmptyLoadingView;
import android.content.Context;
import android.os.Bundle;
 
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends BaseActivity implements LoaderCallbacks<BaseResult>{
	
	static final String TAG = "MainActivity";
	static final int LOADER_HOME_SLIDER_ID = 0;
	static final int LOADER_HOME_HOT_ID = 1;
	static final int LOADER_HOME_MENU_ID = 2;
	static final int LOADER_HOME_MEMBER_ID = 3;
	 
	EmptyLoadingView mLoadingView;
	TextView slider;
	TextView hot;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		getSupportLoaderManager().initLoader(LOADER_HOME_SLIDER_ID, null, this);
		getSupportLoaderManager().initLoader(LOADER_HOME_HOT_ID, null, this);
		//getSupportLoaderManager().initLoader(LOADER_HOME_MEMBER_ID, null, this);

		slider = (TextView) findViewById(R.id.txt_slider);
		hot = (TextView) findViewById(R.id.txt_hot);
		mLoadingView 	= ((EmptyLoadingView)findViewById(R.id.loading));
		TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
		Toast.makeText(this, telephonyManager.getDeviceId(), Toast.LENGTH_LONG).show();;
	}
	
	
	
	public void onClick_GoHomePage(View v)
	 {
	    showContent();
	 }

	 
	 

	@Override
	public Loader<BaseResult> onCreateLoader(int id, Bundle arg1) {
		switch (id) {
		case LOADER_HOME_SLIDER_ID:
			BaseLoader homeSliderLoader = new HomeSliderLoader(this);
			return homeSliderLoader;
			 
		case LOADER_HOME_HOT_ID:
			BaseLoader homeHotLoader = new HomeHotLoader(this);
			homeHotLoader.setProgressNotifiable(mLoadingView);
			return homeHotLoader;
			 
		default:
			homeSliderLoader = new HomeSliderLoader(this);
			return homeSliderLoader;
		}
	}



	@Override
	public void onLoadFinished(Loader<BaseResult> loader, final BaseResult result) {
		final int id = loader.getId();
		Log.i(TAG,"onLoadFinished id:" + id);
		runOnUiThread(new Runnable()
	    {

			@Override
			public void run() {
				
				switch (id) {
				case LOADER_HOME_SLIDER_ID:
					slider.setText(((HomeSliderLoader.Result)result).homeSliderTxt);
					break;
				case LOADER_HOME_HOT_ID:
					hot.setText(((HomeHotLoader.Result)result).homehotTxt);
				default:
					break;
				}
				
			}
			
	    });
		
		
	}



	@Override
	public void onLoaderReset(Loader<BaseResult> loader) {
		// TODO Auto-generated method stub
		
	}

}
